package com.lyh.dt.server;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.jms.Destination;
import javax.jms.Queue;
import javax.jms.Topic;

/**
 * ActiveMq生产者
 * @author liuyuhong
 */
@Component
//@ConditionalOnProperty(prefix = "spring.activemq.jms", name = "enable",havingValue = "true")
public class DtActiveMqProducterService {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    @Qualifier("queueToTm")
    private Queue queueToTm;

    @Autowired
    @Qualifier("topicToRm")
    private Topic topicToRm;

    /**
     * 发送队列消息
     * @author liuyuhong
     * @param bean
     */
    public void sendQueue(DtActiveMqBean bean) {
        System.out.println(String.format("activeMq 使用 queue 模式发送消息：%s", bean.toString()));
        sendMessage(queueToTm, bean);
    }

    /**
     * 发送主题消息
     * @author liuyuhong
     * @param bean
     */
    public void sendTopic(DtActiveMqBean bean){
        System.out.println(String.format("activeMq 使用 topic 模式发送消息：%s", bean));
        sendMessage(topicToRm, bean);
    }


    // 发送消息，destination是发送到的队列，message是待发送的消息
    private void sendMessage(Destination destination, String message){
        jmsMessagingTemplate.convertAndSend(destination, message);
    }

    // 发送消息，destination是发送到的队列，message是待发送的消息
    private void sendMessage(Destination destination, DtActiveMqBean bean){
        System.out.println("activeMq 发送自定义Bean");
        jmsMessagingTemplate.convertAndSend(destination, bean);
    }
}
